<div class="layui-fluid" id="VIEW-list-table" lay-title="通知公告">
    <div class="layui-row layui-col-space12">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body nepadmin-table-full">
                    <div class="layui-btn-container nepadmin-pad-t10 nepadmin-pad-l10">
                        <div class="layui-btn layui-btn-sm add">添加</div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary delete-action" data-type="del">删除</div>
                    </div>
                    <table id="list-table-notice" lay-filter="list-table-notice"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['admin', 'table', 'form', 'jquery'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , admin = layui.admin;
        var view = $('#VIEW-list-table');
        var table = layui.table;
        var tableFilter = 'list-table-notice';
        table.render({
            elem: '[lay-filter="' + tableFilter + '"]',
            url: '/sysNotice/page?token=' + admin.loginToken,
            height: 'full-165',
            cols: [[
                {title: '', type: 'checkbox', fixed: true, width: 60},
                {title: '标题', field: 'title'},
                {title: '状态', width: 120, templet: function (d) {
                        var status = {
                            1: {title: '有效', color: 'blue'},
                            0: {title: '无效', color: 'orange'}
                        }[d.status];
                        return '<span class="layui-badge layui-bg-' + status.color + '">' + status.title + '</span>';
                    }
                },
                {title: '类型', width: 120, templet: function (d) {
                        var status = {
                            1: {title: '通知', color: 'blue'},
                            2: {title: '公告', color: 'orange'}
                        }[d.type];
                        return '<span class="layui-badge layui-bg-' + status.color + '">' + status.title + '</span>';
                    }
                },
                {title: '操作', width: 220, align: 'center', templet: function (d) {
                        var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                        var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return editBtn + delBtn;
                    }
                }
            ]]
        });

        table.on('tool(' + tableFilter + ')', function (obj) {
            if (obj.event === 'del') {//删除行
                del(obj.data.id);
            } else if (obj.event === "edit") {
                admin.popup({
                    url: '/sys/notice/sysNoticeEdit',
                    area: ['60%', '80%'],
                    data : {
                        table:table,
                        id : obj.data.id
                    }
                });
                return false
            }
        });

        view.find('.add').click(function () {
            admin.popup({
                url: '/sys/notice/sysNoticeEdit',
                area: ['60%', '80%'],
                data : {
                    table:table
                }
            });
            return false
        });

        view.find('.delete-action').click(function () {
            var checkedCount = table.checkStatus(tableFilter).data;
            if (checkedCount.length == 0) {
                layer.msg('请先选择某行');
                return false;
            }
            // 获取ids
            var ids = [];
            $.each(checkedCount, function(i, v){
                ids.push(v.id);
            });
            del(ids.join(","));
        });

        function del(ids) {
            layer.confirm("你确定删除吗？", {icon: 3, title: '提示'},
                function (index) {//确定回调
                    admin.post({
                        url: '/sysNotice/delete/'+ids+'?_method=delete',
                        success: function (res) {
                            if (res.code == 0) {
                                //admin.modal.success(res.msg);
                                layer.msg(res.msg);
                                layer.closeAll();
                                table.reload('list-table-notice', {
                                    url: '/sysNotice/page?token=' + admin.loginToken,
                                    page: {
                                        curr: 1
                                    }
                                })
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                }, function (index) {//取消回调
                    layer.close(index);
                }
            );
        }
    })
</script>